<?php
date_default_timezone_set('Etc/GMT');

$v_build_version  = '1.3';

include 'lib_zip.php';
include 'lib_func.php';
include 'lib_file.php';

// <editor-fold defaultstate="collapsed" desc="Query variables">
$f_dbserver = isset($_POST['dbserver'   ]) ? trim($_POST['dbserver'  ]) : 'localhost';
$f_dbuser   = isset($_POST['dbuser'     ]) ? trim($_POST['dbuser'    ]) : 'root'     ;
$f_dbpasswd = isset($_POST['dbpasswd'   ]) ? trim($_POST['dbpasswd'  ]) : ''         ;

$f_dbname   = isset($_POST['dbname'     ]) ? trim($_POST['dbname'    ]) : ''         ;
$f_genname  = isset($_POST['genname'    ]) ? trim($_POST['genname'   ]) : ''         ;
$f_mode     = isset($_POST['mode'       ]) ? trim($_POST['mode'      ]) : ''         ;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Predefined variables">
$v_error    = false;
$a_msg      = array();
$v_tabwidth = 4;
$v_tab      = str_repeat(' ', $v_tabwidth);

$a_exclude_columns = array('creatorid', 'updaterid', 'insertdate', 'updatedate');

class Column {
    var $table_schema               ;
    var $table_name                 ;
    var $column_name                ;
    var $ordinal_position           ;
    var $column_default             ;
    var $is_nullable                ;
    var $data_type                  ;
    var $character_maximum_length   ;
    var $character_octet_length     ;
    var $numeric_precision          ;
    var $numeric_scale              ;
    var $character_set_name         ;
    var $collation_name             ;
    var $column_type                ;
    var $column_key                 ;
    var $extra                      ;
    var $privileges                 ;
    var $column_comment             ;
    var $arguments                  ;
}

// Used to map mysql data types to php data types
$a_datatypes = array(
    'tinyint'   => 'int'    ,
    'smallint'  => 'int'    ,
    'mediumint' => 'int'    ,
    'int'       => 'int'    ,
    'bigint'    => 'int'    ,
    'decimal'   => 'double' ,
    'float'     => 'double' ,
    'double'    => 'double' ,
    'real'      => 'double' ,
    'bit'       => 'int'    ,
    'boolean'   => 'boolean',
    'serial'    => 'int'    ,
    'date'      => 'string' ,
    'datetime'  => 'string' ,
    'timestamp' => 'int'    ,
    'time'      => 'string' ,
    'year'      => 'string' ,
    'char'      => 'string' ,
    'varchar'   => 'string' ,
    'tinytext'  => 'string' ,
    'text'      => 'string' ,
    'mediumtext'=> 'string' ,
    'longtext'  => 'string' ,
);

$a_datatypes_align = array(
    'int'       => 'int    ',
    'string'    => 'string ',
    'double'    => 'double ',
    'float'     => 'float  ',
    'boolean'   => 'boolean',
);
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generate files">
if ($f_mode == 'generate') {
    // <editor-fold defaultstate="collapsed" desc="Validation">
    if ($f_dbserver == '') {
        $v_error = true;
        $a_msg[] = 'You have to input Server';
    }

    if ($f_dbuser == '') {
        $v_error = true;
        $a_msg[] = 'You have to input Username';
    }

    if ($f_dbname == '') {
        $v_error = true;
        $a_msg[] = 'You have to input Database Name';
    }
    // </editor-fold>

    if(!$v_error) {
        $db_conn = @mysqli_connect($f_dbserver, $f_dbuser, $f_dbpasswd, 'information_schema');

        if (!$db_conn) {
            $v_error = true;
            $a_msg[] = 'Cannot connect to Database (' . mysqli_connect_error() . ')';
        } else {
            mysqli_query($db_conn, 'SET NAMES utf8;');

            $f_dbname = strtolower($f_dbname);

            $f_dbname   = mysqli_real_escape_string($db_conn, $f_dbname);
            $db_query   = "
                SELECT  schema_name
                FROM    schemata
                WHERE   schema_name = '$f_dbname'
            ";
            $db_result  = mysqli_query($db_conn, $db_query);

            if ($db_row = mysqli_fetch_object($db_result)) {
                $d_columns    = array();
                $d_allcolumns = array();

                // <editor-fold defaultstate="collapsed" desc="Get database structure">
                $db_query   = "
                    SELECT  table_schema, table_name, column_name, ordinal_position,
                            column_default, is_nullable, data_type, character_maximum_length,
                            character_octet_length, numeric_precision, numeric_scale,
                            character_set_name, collation_name, column_type, column_key,
                            extra, privileges, column_comment
                    FROM    columns
                    WHERE   table_schema = '$f_dbname'
                    ORDER BY
                            table_name, ordinal_position, column_name
                ";
                $db_result  = mysqli_query($db_conn, $db_query);

                while($db_row = mysqli_fetch_object($db_result)) {
                    /* @var $db_row Column */

                    // [v1.13] Support options in COLUMN COMMENTS
                    if($db_row->column_comment) {
                        if(preg_match('/^[^=]*=(.*)$/', $db_row->column_comment)) {
                            $a_parts = deserialize_query_string($db_row->column_comment);

                            $db_row->column_comment = isset($a_parts['COMMENT']) ? $a_parts['COMMENT'] : '';
                            unset($a_parts['COMMENT']);

                            if(!empty($a_parts)) {
                                foreach($a_parts as $v_key => $v_arg) {
                                    $v_key = strtolower($v_key);
                                    $db_row->arguments[$v_key] = $v_arg;
                                }
                            }
                        }
                    }

                    $d_columns[$db_row->table_name][$db_row->column_name]             = $db_row;
                    $d_allcolumns[$db_row->column_name]['types'][$db_row->data_type]  = $db_row->data_type;
                    $d_allcolumns[$db_row->column_name]['table'][$db_row->table_name] = $db_row->table_name;
                }

                mysqli_close($db_conn);
                // </editor-fold>

                $f_dbname   = @preg_replace('/[^a-zA-Z0-9_]/', '', $f_dbname );
                $f_genname  = @preg_replace('/[^a-zA-Z0-9_]/', '', $f_genname);
                $f_dbname   = strtolower($f_dbname  );
                $f_genname  = strtolower($f_genname );

                if ($f_genname == '') {
                    $f_genname = $f_dbname;
                }

                $o_zip         = new zipfile();

                // <editor-fold defaultstate="collapsed" desc="tbl_TABLE_DATABASE.php">
                foreach ($d_columns as $v_table => $a_columns) {
                    $v_table          = strtolower($v_table);
                    $v_classname      = strtolower($v_table);
                    $v_classname      = $f_genname . '_' . $v_table;
                    $v_tablename      = preg_replace('/_/', ' ', $v_table);
                    $v_tablename      = ucwords($v_tablename);
                    $v_table_content  = '';
                    $v_column_assign  = '';
                    $v_column_get     = '';
                    $v_columns_array  = '';

                    $v_table_content .= "<?php\n";
                    $v_table_content .= "\$a_tablecolumns_{$f_genname}_{$v_table} = array(\n";
                    $v_table_content .= "{$v_tab}'name'    => '$v_table',\n";
                    $v_table_content .= "{$v_tab}'columns' => array(\n";

                    $v_columns_array .= "{$v_tab}return array(\n";
                    $v_columns_array .= "{$v_tab}{$v_tab}'name'    => '$v_table',\n";
                    $v_columns_array .= "{$v_tab}{$v_tab}'columns' => array(\n";

                    $v_maxlength = max(array_map('strlen', array_keys($a_columns))) + 1;

                    $a_strings          = array();
                    $a_numbers          = array();
                    $a_datetimes        = array();
                    $a_dates            = array();
                    $a_column_comments  = array();
                    $a_column_instances = array();

                    foreach ($a_columns as $idx => $o_column) {
                        $v_datatype = isset($a_datatypes[$o_column->data_type]) ? $a_datatypes[$o_column->data_type] : 'string';
                        $v_comment  = $o_column->column_comment;
                        $v_comment  = trim($v_comment);
                        $v_comment  = check_plain($v_comment ? $v_comment : $o_column->column_name);

                        if(!isset($o_column->arguments['instance'])) {
                            $o_column->arguments['instance'] = $v_datatype;
                        }

                        $o_column->column_comment = $v_comment;

                        $a_column_comments[]   = $v_comment;
                        $a_column_instances[]  = isset($o_column->arguments['instance']) ? $o_column->arguments['instance'] : $v_datatype;
                        $a_columns[$idx]       = $o_column;
                    }

                    $v_maxcommentlength  = max(array_map(function($v_value) {
                        return mb_strlen($v_value, 'UTF-8');
                    }, $a_column_comments)) + 1;

                    $v_maxinstancelength = max(array_map('strlen', $a_column_instances)) + 1;

                    foreach ($a_columns as $o_column) {
                        $v_length   = strlen($o_column->column_name);
                        $v_default  = $o_column->column_default;
                        $v_instance = $o_column->arguments['instance'];
                        $v_datatype = isset($a_datatypes[$o_column->data_type]) ? $a_datatypes[$o_column->data_type] : 'string';
                        $a_comments = array();

                        if ($o_column->data_type == 'date') {
                            $a_dates[] = $o_column->column_name;
                        }
                        elseif ($o_column->data_type == 'datetime') {
                            $a_datetimes[] = $o_column->column_name;
                        }
                        elseif ($v_datatype == 'int' || $v_datatype == 'float' || $v_datatype == 'double') {
                            $a_numbers[] = $o_column->column_name;
                        }
                        elseif ($v_datatype == 'string') {
                            $a_strings[] = $o_column->column_name;
                        }

                        if (($v_datatype == 'int' || $v_datatype == 'float' || $v_datatype == 'double') && $v_default !== '' && $v_default != null) {
                            $v_function = "{$v_datatype}val";
                            $v_default  = $v_function($v_default);
                        } else {
                            $v_default  = "'$v_default'";
                        }

                        if ($o_column->column_key == 'PRI') {
                            $a_comments[] = 'Primary';
                        }
                        elseif ($o_column->column_key == 'UNI') {
                            $a_comments[] = 'Unique';
                        }

                        if ($v_datatype == 'int' || $v_datatype == 'float' || $v_datatype == 'double') {
                            if (preg_match('/\bunsigned\b/', $o_column->column_type)) {
                                $a_comments[] = 'Unsigned';
                            }
                        }

                        if ($o_column->extra) {
                            $v_extra = preg_replace('/_/', ' ', $o_column->extra);
                            $v_extra = ucwords($v_extra);
                            $a_comments[] = $v_extra;
                        }

                        if ($o_column->data_type == 'text') {
                            $a_comments[] = 'Text';
                        }
                        elseif ($o_column->data_type == 'mediumtext') {
                            $a_comments[] = 'Medium Text';
                        }
                        elseif ($o_column->data_type == 'longtext') {
                            $a_comments[] = 'Long Text';
                        }
                        elseif ($o_column->character_maximum_length) {
                            $a_comments[] = 'Max Length (' . $o_column->character_maximum_length . ')';
                        }

                        $v_spacelen         = $v_maxlength - $v_length;
                        $v_spacelen         = $v_spacelen > 0 ? $v_spacelen : 1;
                        $v_spaces           = str_repeat(' ', $v_spacelen);

                        $v_commentlength    = mb_strlen($o_column->column_comment, 'UTF-8');
                        $v_commentspacelen  = $v_maxcommentlength - $v_commentlength;
                        $v_commentspacelen  = $v_commentspacelen > 0 ? $v_commentspacelen : 1;
                        $v_commentspaces    = str_repeat(' ', $v_commentspacelen);

                        $v_instancelength   = strlen($v_instance);
                        $v_instancespacelen = $v_maxinstancelength - $v_instancelength;
                        $v_instancespacelen = $v_instancespacelen > 0 ? $v_instancespacelen : 1;
                        $v_instancespaces   = str_repeat(' ', $v_instancespacelen);

                        $v_datacomment      = empty($a_comments) ? '' : ('[' .  implode(' | ', $a_comments) . ']');

                        $v_table_content .= "{$v_tab}{$v_tab}'{$o_column->column_name}'" . $v_spaces . ",\n";
                        $v_columns_array .= "{$v_tab}{$v_tab}{$v_tab}'{$o_column->column_name}'" . $v_spaces . ",\n";

                        if (!in_array($o_column->column_name, $a_exclude_columns)) {
                            if ($v_datatype == 'int' || $v_datatype == 'float' || $v_datatype == 'double') {
                                $v_column_assign .= "\$f_{$o_column->column_name}" . $v_spaces . "= 0 ;\n";
                            }
                            else {
                                $v_column_assign .= "\$f_{$o_column->column_name}" . $v_spaces . "= '';\n";
                            }

                            $v_column_get    .= "if (isset(\$_REQUEST['{$o_column->column_name}'" . $v_spaces . "] )) { \$f_{$o_column->column_name}" . $v_spaces . "= \$_REQUEST['{$o_column->column_name}'" . $v_spaces . "] ; }\n";
                        }
                    }

                    $v_columns_array .= "{$v_tab}{$v_tab}),\n";
                    $v_columns_array .= "{$v_tab});\n";

                    $v_table_content .= "{$v_tab}),\n";
                    $v_table_content .= ");\n\n";

                    $v_table_content .= "function get_columns_{$f_genname}_{$v_table}() {\n";
                    $v_table_content .= $v_columns_array;
                    $v_table_content .= "}\n\n";

                    $v_table_content .= $v_column_assign;

                    $v_spacelen = $v_maxlength - strlen('creatorid');
                    $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                    $v_table_content .= "\$f_creatorid" . str_repeat(' ', $v_spacelen) . "= '';\n";

                    $v_spacelen = $v_maxlength - strlen('updaterid');
                    $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                    $v_table_content .= "\$f_updaterid" . str_repeat(' ', $v_spacelen) . "= '';\n";

                    $v_spacelen = $v_maxlength - strlen('insertdate');
                    $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                    $v_table_content .= "\$f_insertdate" . str_repeat(' ', $v_spacelen) . "= date('Y-m-d H:i:s');\n";

                    $v_spacelen = $v_maxlength - strlen('updatedate');
                    $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                    $v_table_content .= "\$f_updatedate" . str_repeat(' ', $v_spacelen) . "= \$f_insertdate;\n";

                    $v_table_content .= "\n";
                    $v_table_content .= 'class ' . $v_classname . " {\n";
                    $v_table_content .= "{$v_tab}public function __get(\$v_property) {\n";
                    $v_table_content .= "{$v_tab}{$v_tab}return '';\n";
                    $v_table_content .= "{$v_tab}}\n";
                    $v_table_content .= "} \n\n";

                    $v_table_content .= "\$d_$v_table = new $v_classname();\n\n";
                    $v_table_content .= "$v_column_get\n";

                    // <editor-fold defaultstate="collapsed" desc="make_filter()">
                    /**
                     * FUNCTION: MAKE_FILTER
                     */
                    $v_table_content .= "function make_filter_{$f_genname}_{$v_table}(\$s_field, \$s_keyword) {\n";
                    $v_table_content .= "{$v_tab}\$v_filter = '';\n\n";
                    $v_table_content .= "{$v_tab}if (\$s_field != '' && \$s_keyword != '') {\n";
                    $v_table_content .= "{$v_tab}{$v_tab}switch (\$s_field) {\n";

                    if (!empty($a_strings) || !empty($a_numbers)) {
                        foreach ($a_strings as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }

                        foreach ($a_numbers as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }

                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \"`\$s_field` like '%\$s_keyword%'\";\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}break;\n\n";
                    }

                    if (!empty($a_datetimes)) {
                        foreach ($a_datetimes as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }

                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_thisday = \$v_nextday = date('Y-m-d');\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (strpos(\$s_keyword, '~') !== FALSE) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_keywords = explode('~', \$s_keyword)   ;\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_thisday  = format_date(\$v_keywords[0]);\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_nextday  = format_date(\$v_keywords[1]);\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \"(`\$s_field` >= '\$v_thisday 00:00:00' and `\$s_field` <= '\$v_nextday 23:59:59')\";\n";
                        $v_table_content .= "\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}break;\n\n";
                    }

                    if (!empty($a_dates)) {
                        foreach ($a_dates as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }


                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_thisday = \$v_nextday = date('Y-m-d');\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (strpos(\$s_keyword, '~') !== FALSE) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_keywords = explode('~', \$s_keyword)   ;\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_thisday  = format_date(\$v_keywords[0]);\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_nextday  = format_date(\$v_keywords[1]);\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \"(`\$s_field` >= '\$v_thisday' and `\$s_field` <= '\$v_nextday')\";\n";
                        $v_table_content .= "\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}break;\n\n";
                    }

                    $v_table_content .= "{$v_tab}{$v_tab}}\n";
                    $v_table_content .= "{$v_tab}}\n\n";
                    $v_table_content .= "{$v_tab}return \$v_filter;\n";
                    $v_table_content .= "}\n\n";
                    // </editor-fold>

                    // <editor-fold defaultstate="collapsed" desc="make_advanced_filter()">
                    /**
                     * FUNCTION: MAKE_ADVANCED_FILTER
                     */
                    $v_table_content .= "function make_advanced_filter_{$f_genname}_{$v_table}(\$s_field, \$s_keyword) {\n";
                    $v_table_content .= "{$v_tab}\$v_filter = '';\n\n";
                    $v_table_content .= "{$v_tab}if (\$s_field != '' && \$s_keyword != '') {\n";
                    $v_table_content .= "{$v_tab}{$v_tab}switch (\$s_field) {\n";

                    if (!empty($a_strings)) {
                        foreach ($a_strings as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }

                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \"`\$s_field` like '%\$s_keyword%'\";\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}break;\n\n";
                    }

                    if (!empty($a_numbers)) {
                        foreach ($a_numbers as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }

                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (strpos(\$s_keyword, '~') !== FALSE) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}list(\$v_from, \$v_to) = explode('~', \$s_keyword, 2);\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (is_numeric(\$v_from)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \$v_filter . \"`\$s_field` >= \$v_from\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (is_numeric(\$v_to)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \$v_filter . (empty(\$v_filter) ? '' : ' and ') . \"`\$s_field` <= \$v_to\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = empty(\$v_filter) ? '' : \"(\$v_filter)\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}elseif (is_numeric(\$s_keyword)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \"`\$s_field` = \$s_keyword\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}break;\n\n";
                    }

                    if (!empty($a_datetimes)) {
                        foreach ($a_datetimes as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }

                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (strpos(\$s_keyword, '~') !== FALSE) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}list(\$v_fromday, \$v_today) = explode('~', \$s_keyword, 2);\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (validate_date(\$v_fromday)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \$v_filter . \"`\$s_field` >= '\$v_fromday 00:00:00'\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (validate_date(\$v_today)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \$v_filter . (empty(\$v_filter) ? '' : ' and ') . \"`\$s_field` <= '\$v_today 23:59:59'\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = empty(\$v_filter) ? '' : \"(\$v_filter)\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}elseif (validate_date(\$s_keyword)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \"(`\$s_field` >= '\$s_keyword 00:00:00' and `\$s_field` <= '\$s_keyword 23:59:59')\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}break;\n\n";
                    }

                    if (!empty($a_dates)) {
                        foreach ($a_dates as $v_columname) {
                            $v_length   = strlen($v_columname);
                            $v_spacelen = $v_maxlength - $v_length;
                            $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                            $v_spaces   = str_repeat(' ', $v_spacelen);

                            $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}case '$v_columname'$v_spaces:\n";
                        }

                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (strpos(\$s_keyword, '~') !== FALSE) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}list(\$v_fromday, \$v_today) = explode('~', \$s_keyword, 2);\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (validate_date(\$v_fromday)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \$v_filter . \"\$s_field >= '\$v_fromday'\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}if (validate_date(\$v_today)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \$v_filter . (empty(\$v_filter) ? '' : ' and ') . \"\$s_field <= '\$v_today'\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = empty(\$v_filter) ? '' : \"(\$v_filter)\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}elseif (validate_date(\$s_keyword)) {\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}{$v_tab}\$v_filter = \"\$s_field = '\$s_keyword'\";\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}}\n\n";
                        $v_table_content .= "{$v_tab}{$v_tab}{$v_tab}{$v_tab}break;\n\n";
                    }

                    $v_table_content .= "{$v_tab}{$v_tab}}\n";
                    $v_table_content .= "{$v_tab}}\n\n";
                    $v_table_content .= "{$v_tab}return \$v_filter;\n";
                    $v_table_content .= "}\n\n";
                    // </editor-fold>

                    // <editor-fold defaultstate="collapsed" desc="build_filter()">
                    /**
                     * FUNCTION: BUILD_FILTER
                     */
                    $v_table_content .= "function build_filter_{$f_genname}_{$v_table}(\$s_field) {\n";

                    $a_build_filters = array();

                    foreach ($a_columns as $o_column) {
                        /* @var $o_column Column */
                        $v_length   = strlen($o_column->column_name);
                        $v_spacelen = $v_maxlength - $v_length;
                        $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                        $v_spaces   = str_repeat(' ', $v_spacelen);

                        $v_commentlength   = mb_strlen($o_column->column_comment, 'UTF-8');
                        $v_commentspacelen = $v_maxcommentlength - $v_commentlength;
                        $v_commentspacelen = $v_commentspacelen > 0 ? $v_commentspacelen : 1;
                        $v_commentspaces   = str_repeat(' ', $v_commentspacelen);

                        $a_build_filters[$o_column->ordinal_position] = "{$v_tab}echo '<option value=\"{$o_column->column_name}\"'$v_spaces, (\$s_field == '{$o_column->column_name}'$v_spaces ? ' selected=\"selected\"' : ''), '> {$o_column->column_comment}{$v_commentspaces}</option>';\n";
                    }

                    ksort($a_build_filters);

                    $v_table_content .= implode('', $a_build_filters);
                    $v_table_content .= "}\n\n";
                    // </editor-fold>

                    // <editor-fold defaultstate="collapsed" desc="build_advanced_filter()">
                    /**
                     * FUNCTION: BUILD_ADVANCED_FITLER
                     */
                    $v_table_content .= "function build_advanced_filter_{$f_genname}_{$v_table}(\$s_field, \$a_hide = array()) {\n";
                    $v_table_content .= "{$v_tab}if (empty(\$a_hide)) {\n";
                    $v_table_content .= "{$v_tab}{$v_tab}return build_filter_{$f_genname}_{$v_table}(\$s_field);\n";
                    $v_table_content .= "{$v_tab}}\n";
                    $v_table_content .= "\n";
                    $v_table_content .= "{$v_tab}\$a_columns = array(\n";

                    $a_build_filters = array();

                    foreach ($a_columns as $o_column) {
                        /* @var $o_column Column */
                        $v_length   = strlen($o_column->column_name);
                        $v_spacelen = $v_maxlength - $v_length;
                        $v_spacelen = $v_spacelen > 0 ? $v_spacelen : 1;
                        $v_spaces   = str_repeat(' ', $v_spacelen);

                        $v_commentlength   = mb_strlen($o_column->column_comment, 'UTF-8');
                        $v_commentspacelen = $v_maxcommentlength - $v_commentlength;
                        $v_commentspacelen = $v_commentspacelen > 0 ? $v_commentspacelen : 1;
                        $v_commentspaces   = str_repeat(' ', $v_commentspacelen);

                        $a_build_filters[$o_column->ordinal_position] = "{$v_tab}{$v_tab}'{$o_column->column_name}'$v_spaces=> '{$o_column->column_comment}'$v_commentspaces,\n";
                    }
                    ksort($a_build_filters);

                    $v_table_content .= implode('', $a_build_filters);
                    $v_table_content .= "{$v_tab});\n\n";
                    $v_table_content .= "{$v_tab}\$a_hide    = array_combine(\$a_hide, \$a_hide);\n";
                    $v_table_content .= "{$v_tab}\$a_columns = array_diff_key(\$a_columns, \$a_hide);\n\n";
                    $v_table_content .= "{$v_tab}foreach (\$a_columns as \$v_columnname => \$v_columncomment) {\n";
                    $v_table_content .= "{$v_tab}{$v_tab}echo '<option value=\"', \$v_columnname, '\"', (\$s_field == \$v_columnname ? ' selected=\"selected\"' : ''), '>', \$v_columncomment, '</option>';\n";
                    $v_table_content .= "{$v_tab}}\n";

                    $v_table_content .= "}\n";
                    // </editor-fold>

                    $v_table_content .= '?>';

                    $o_zip->addFile($v_table_content, "tbl_{$f_genname}_{$v_table}.php");
                    namhoa_writefile("tbl_{$f_genname}_{$v_table}.php", $v_table_content);
                }
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="lib_conn_DATABASE.php">
                /**
                 * Datase Connection
                 */
                $v_lib_connect  = "<?php\n";
                $v_lib_connect .= "\$db_conn_{$f_genname} = db_connect(\$db_server_{$f_genname}, \$db_user_{$f_genname}, \$db_passwd_{$f_genname}, \$db_name_{$f_genname});\n";
                $v_lib_connect .= "?>";

                $o_zip->addFile($v_lib_connect, "lib_conn_{$f_genname}.php");
                namhoa_writefile("lib_conn_{$f_genname}.php", $v_lib_connect);
                // </editor-fold>

                // <editor-fold defaultstate="collapsed" desc="lib_db_DATABASE.php">
                /**
                 * DB Connection Settings
                 */
                $v_db_conf  = "<?php\n";
                $v_db_conf .= "\$db_server_{$f_genname} = 'localhost';\n";
                $v_db_conf .= "\$db_user_{$f_genname}   = 'root';\n";
                $v_db_conf .= "\$db_passwd_{$f_genname} = '.';\n";
                $v_db_conf .= "\$db_name_{$f_genname}   = '{$f_genname}';\n";
                $v_db_conf .= "\$db_conn_{$f_genname}   = null;\n";
                $v_db_conf .= "?>";

                $o_zip->addFile($v_db_conf, "lib_db_{$f_genname}.php");
                namhoa_writefile("lib_db_{$f_genname}.php", $v_db_conf);
                // </editor-fold>

                $v_zip = $o_zip->file();

                if (headers_sent()) {
                    $v_error = true;
                    $a_msg[] = "Header is already sent";
                }
                else {
                    header("Content-Disposition: attachment; filename=db_{$f_genname}.zip");
                    header('Accept-Ranges: bytes');
                    header('Content-Transfer-Encoding: binary');
                    header("Content-Length: " . strlen($v_zip));
                    header('Connection: close');
                    header("Content-Type: application/zip");

                    echo $v_zip;
                    exit(0);
                }
            } else {
                $v_error = true;
                $a_msg[] = "Database `$f_dbname` does NOT exist";
            }
        }
    }
}
// </editor-fold>

?>
<!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>
<html>
    <head>
        <title>DAL Generator</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <link rel="shortcut icon" type="image/png"  href="data:image/png;base64,
            iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAA
            AAlwSFlzAAAN1wAADdcBQiibeAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9y
            Z5vuPBoAAAKVSURBVDiNhZM/SBtxFMe/98tdEnPhLqWV5HIpQis42UlcbCE6FCIkECFL
            Bw0loIsBK0SK15KaK1lSQTfnVtx0TJeUwg2l7VApFOw/EBsTvWAoShJzSX6/Do1QqJK3
            veH7eY/3/T4uk8mgV2ma5gAgAOgAIAA6uq6fo9v0EtsVRZkghLwSRTFOCHnp9/uDmqbZ
            AYDvNVlRlIlqtTrKGPter9dHARRPTk7uKorClcvlN7024E3TjFiWZZckqRGNRvccDodl
            WZbbNM0HAPheADo3N/cewP74+Pjh9vZ2IRKJ/ATwKx6PGwAod3FETdM4AByAvu5tKADO
            ZrM9ppR+JYQEBEE4bbVa1ymlZULIUKfTeUq6Yn55edmXy+VuyLIc4ThuVZbl8NbWlnNt
            be1LOBx2U0oPms2mQCk9mJyc7NvY2PgAoAPGGLe+vt7P8/wLQsgqIeQZgBgh5AkhJDsw
            MPB8aWkp5Pf7kysrKwlFUR6l0+n7jDFfJpMBxxjjdnZ2+mdnZx9WKpVjAE0ANgCnAIiq
            qt58Pv92eHj4FoAfAAYBfNJ1vQwAHGNM9Hq90bOzM6XRaBwGg8FrqVTqdzabdRmGsS+K
            ok+SpHqpVHqt63rtP5sAENM0RwDsAnDGYrFyKBT6WCwWRwzDcNVqtVKtVhvBFaEjAGg6
            nd4TRdED4HxhYWFIVdWh+fn5QQCWJEm+XC531HXlUkBjenr6nSzLAgDesqxqqVS612w2
            KwDsHo/HPTU1tQugcRWA5fP5Y0EQnKqq9oui6ATwze12uwKBwE2e512FQuEIALsMYON5
            HouLi62ZmRlHMpmstttth2EY3lQqdba5uVlMJBLm2NjYnq7r7csA/ybx4rFuA7gD4DP+
            2sZdJQaAP0XhCVX9WNFfAAAAAElFTkSuQmCC" />

        <style type="text/css">
            html, body {margin:0;padding:0;}
            body {font-family:Georgia,'Times New Roman','Bitstream Charter',Times,serif;background: #F5F5F5;padding:20px;}
            h1 {line-height:48px ;color:#636363; font-weight:normal;margin-top:0;}
            h1 img {vertical-align:top;}
            img{-webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-o-user-select: none;user-select: none;}
            label {clear: both; float: left; display: block; width: 150px;line-height:25px;}
            #message_box {border-style: solid; border-width: 2px 0; border-color: #E10C0C; padding:7px 10px 7px 38px; line-height: 20px; position: relative; min-height: 24px; margin-bottom: 30px; background: #FFCDD1 url() 10px 7px no-repeat;}
            #message_box ul, #message_box li {margin:0; padding:0; list-style: none;line-height:24px;}
            .code {font-family:Consolas,'Lucida Console','DejaVu Sans Mono',monospace; font-size: 13px;}
            .grey {color:#bbb;}
            input[type=text],input[type=password]{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;-ms-box-sizing:border-box;-o-box-sizing:border-box;box-sizing:border-box;-o-border-radius:3px;outline:none;border-style:solid;border-width:1px;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;background:#fff;border-color:#C0C0C0 #D9D9D9 #D9D9D9;padding:3px;height: 25px;line-height: 25px;}
            input[type=text]:hover,input[type=password]:hover{-moz-box-shadow:0 1px 1px rgba(0,0,0,0.1);-o-box-shadow:0 1px 1px rgba(0,0,0,0.1);-webkit-box-shadow:0 1px 1px rgba(0,0,0,0.1);-ms-box-shadow:0 1px 1px rgba(0,0,0,0.1);box-shadow:0 1px 1px rgba(0,0,0,0.1);border-color:#A0A0A0 #B9B9B9 #B9B9B9}
            .button{padding:5px 12px 5px 10px;border:1px solid #3079ED;color:#fff;line-height:24px;display:inline-block;cursor:pointer;margin-top:20px;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;-ms-box-sizing:border-box;-o-box-sizing:border-box;box-sizing:border-box;-webkit-border-radius:3px;-moz-border-radius:3px;-ms-border-radius:3px;-o-border-radius:3px;border-radius:3px;background:#4D90FE;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#4D90FE',endColorstr='#4787ED');background:-webkit-gradient(linear, center top, center bottom, from(#4D90FE), to(#4787ED));background:-webkit-linear-gradient(centertop,#4D90FE,#4787ED);background:-moz-linear-gradient(center top , #4D90FE, #4787ED);background:-ms-linear-gradient(centertop,#4D90FE,#4787ED);background:-o-linear-gradient(centertop,#4D90FE,#4787ED);-webkit-box-shadow:0 1px 2px rgba(0,0,0,0.3) inset;-moz-box-shadow:0 1px 2px rgba(0,0,0,0.3) inset;-o-box-shadow:0 1px 2px rgba(0,0,0,0.3) inset;-ms-box-shadow:0 1px 2px rgba(0,0,0,0.3) inset;box-shadow:0 1px 2px rgba(0,0,0,0.3) inset;}
            .button:hover{background:#357AE8;filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#4D90FE',endColorstr='#357AE8');background:-webkit-gradient(linear, center top, center bottom, from(#4D90FE), to(#357AE8));background:-webkit-linear-gradient(centertop,#4D90FE,#357AE8);background:-moz-linear-gradient(center top , #4D90FE, #357AE8);background:-ms-linear-gradient(centertop,#4D90FE,#357AE8);background:-o-linear-gradient(centertop,#4D90FE,#357AE8)}
            .button img{vertical-align:middle;margin:0 2px 0 0;padding:0}
            pre {line-height:1.4em;}
        </style>
    </head>
    <body>
        <h1>
            <img src="
                 AABXAvmHAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAG7AAABuwBHnU4NQAAABl
                 0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAyZSURBVGiB1ZrLix
                 x1u8c/da+urumepJ0zSYySxITkVTcihIgEURcnBkXOMhsR/QPUBA64OAwNLl5dC
                 cJBxIXgwoXucox4EIkJuBAFEx1yI28ywZkMY5Lutqfut7OYeX6nuqbj5ixeT0HR
                 NXX51XP5Pt/nUqP1+33+P2/6P1uA/+tm1v9YWFgwABew+espVwIpEPf7/UJOKgU
                 WFhacZ555Ztd77733H71e77jruvOappEkCY7jEMcxVVXhui5pmlKWJZZlYRgGcR
                 xTFAVVVVFVFXmeU5YleZ6rt8u1oijQNA3DMDBNkzzPyfMc27bRNI00TXEcB03Tq
                 KqKVqtFlmWUZfnbvXv3/uvkyZN/X1hYWO33+wmA1u/3xfJz33333X/u37//39I0
                 Rdd1kiTBNE3CMMS2bcqyVAoB6rplWWRZRpIkpGlKHMfkeU6WZUoBTdPQNA1d17E
                 sSykKYNs2eZ5TFAWu61IUBZZl0Wq1KIqCVqtFmqa0222uXLny6csvv/zvwO/9fr
                 8QmLSAnbt3736uqiolkOu6JEmC67pkWUZRFMobeZ7jOA5FURCGIVmWYVkWpmliG
                 Aa6rlOWJVVVKcENw0DTNOI4JooiqqrCMAyiKFLrRVGEbdvouq7WjOMY27YZj8fs
                 3r37X4GdmzIrnFtAb3Z2tmuaJmVZ0mq1SJIE3/dJ0xTLstTLHcchz3PiOFZCF0W
                 hBKlb2jRNNE0jz3OiKFJwEy+EYai8s76+TrvdJs9zdF1H13UFrziOcV2Xdrv9L0
                 BvU2algAE4YiHXdcnzHN/3iaKIdrtNmqYbmloWYRgqhcIwpCxLheGiKEjTlCiKC
                 IKAMAyJ43gCThI3cRwr5ZMkwfM8oijCcRyqqtoIUtMkSRKFis3N2ZRZKaABWpZl
                 mKZJVVV4nkeWZXQ6HYU/WdR1XaIoQtM0TNMkyzLSNMU0TWzbVhCqB7BYGVCEYNu
                 2ihuBq+/7iByAIovGpm3uk1QpLpOHRQlxqwSvuDVJEgUHCeq6++uBW1UVn332GW
                 +//TY3b97k7t27nD59WuE7yzIFV0GAYRiUZanYSeKivtXzgC4vlIc0TVOx0G63V
                 YBVVaUCLMsy8jxXlJplGVmWKdhUVUWSJBiGwdLSEp7n8dFHH2GaJrqus7y8zOuv
                 v0632yVNU2ZmZiaUEPyLwWZmZiYMP5HIJADLssQwDKqqQtd1pYTrusRxrK7JsWD
                 YsiwFHVlnMBjwwQcfKGi1221Fybqu43kenueRpimdTocoivA8T71PjJYkCbquUx
                 RFXeQJCGlyQ911gl/XdTFNE9d1lTD1pKbrOmmaUhSFsq6maSwuLtLpdPA8j3a7j
                 ed5PProo2qNy5cvc+rUKa5evTohvG3bhGGoYkzk2MwdWlMBTSAEUBSF0lb4HDYS
                 juM4uK6L67oAKheIwuJJ2Z977jlmZ2exLIuyLHnllVd48cUXOX78OJqmsW3bNrZ
                 v386FCxdotVpEUaRiUbwociRJUpd7axDLjZL2hRbrcWGaJq1WC9d18TwPTdNUEM
                 q9spVlyddff82dO3dU+ZCmKaPRSFGzZVkcOnSI1157TcGkLEvFagLTNE3V++8HI
                 V1eKr8CIUApJqwi2PV9XyWtoigmWKiqKn777Tdc10XTNIIg4JtvvuH27ducOXNG
                 sdfq6irj8Vgpn2WZsrjEm2EY2LYtMTA9iEVwcbcEsWBcPCGB6nmeem4wGEwEmTz
                 37LPPMhqNOH/+PL7vc+nSJRYXF2m32/i+T1VVrK2tMRwO2bFjh2IcMaAQiKxX93
                 BdAX3jnfoElOoPiPWbx57nqaAfDAbkec5gMGBtbY3BYIBpmuzdu5effvqJJEnYv
                 n27SnSdToejR4+SZRk7duxQwSvZWQLYsixF35te08ULdQ9oApc6jYr75FigIVYG
                 aLVa6p719XXeffddBSeAV199lZ07d3Ljxg1eeuklLl68yPLyMt1ul4MHD+I4Dlm
                 W0Wq1VIntOI4iiDokbduGGgttUaAexPVjsXzd+gDj8Zjz589z7Ngx5ubm+P3333
                 FdV5UEf/zxB3Ecc+zYMbrdLj///DNPPfUUZVmya9cuHMdRuca2bVXkSbA6jjMB6
                 c3zUxVQLFJVFWVZqkVEGaXp5j2j0Yg333yTwWDA2bNn2b9/P1999ZXyiMTTF198
                 weHDh1lZWWFlZYV2u82pU6eYm5tT+UBK6HrZXa9spTJo1kUTpURdQBG8/ivHYo0
                 33niDPM+Zn5/n1q1bLC0t0ev1sCxLJTVJYIuLi+i6zq5du4jjmA8//JD3339fea
                 CewZuBKsatwXY6CzUDtfnb5HhN02i32xiGwbZt2yauS+GXZRm2bTM7O6uEkww+O
                 zuLYRhq/fq76nTehHB925KJm9aue0QWlaT01ltvKUXEzXKfFHme5+G67kS3VlUV
                 J0+e3CJ8cxfhmzv3ycTaNKtLc57nuep3wzAkCAJ++eUX1YXJtSiKCMMQx3GYmZk
                 hiiIVW6JgmqZcv36dpsEkzzQR0VSG+wVxmqbkeb7FC2I1uba8vMy1a9f48ssvVb
                 8si1uWxfPPP8+hQ4cYDAbcuXOHc+fOqT5DYHf69Gl83+fAgQM8+OCDW95Zp/Rp5
                 5sKqI5MRiL19lAYQa698847Csv1rSgK9uzZQ7fbZTQacevWLVqtFnv27OHKlSuK
                 RSRjf/zxxwB8+umnStim8E0vbHpRdWQTeUD6WMmCAh+p98XFpmnS7XYn8oQUYPv
                 27VNekcZ///79XLhwQdGjaZrMzs5SliX37t3bYtXm381YmAYhDdCki8rznDAMSd
                 NUNSxSMmdZNtEm1i2T5znD4ZD5+XmiKFLl7/LysoKfZGcJ6DpzNYScgFDjd6oH9
                 PF4zPr6+kS5IO2h1CeapnH06FHW1tZYXV3deHCTwzudDj/++COPPPIIcRyruv6H
                 H37A9/0tAfnQQw9x5MiRqTRZp8/6scja9ADwv5lYvCDn6l7QNI19+/bx2GOPcfb
                 sWW7fvq2U0HWd0WjEJ598woEDB0iShGvXrqHrOnNzcxM0u3v3bk6cOEGr1Zqga7
                 F0E0byXL0i2KJAmqYEQbDlYQlgQA2ihsMhe/fuZWlpCdd11cK9Xo80TVlcXKSqK
                 nzfV7NO2ZIk4cCBA6ysrNDpdOh2u3iep9aYZv1pDNRUQLFQs2wWmpR2T7qlc+fO
                 KQ81s3Cv11MvlOv1huXMmTOcOHGCqqqUd6U0l/fWvSHHmyXF9CCWQKtrKywkdFr
                 vltbX1+n1ekq4ZlKaVtMA+L7PvXv3CIJAEYAwnuu6qowWgZtrw/TBll4PMJkup2
                 mq2KSeH2zb5oUXXlDYl4nF/YR2HEeNHwGOHz+uoDkYDAjDkMFgQBAEBEGgLC9K1
                 BmM+wSxJiOSafP9Oo0JSz3wwAM8/fTTLC0t8fjjj9Pr9bh69Srff/+9qnOyLOPJ
                 J5/k4MGDBEHA4uIiDz/8MDMzM5RlSRiGeJ7HcDiUoRXtdls1OOIdQcWfQUiN8ET
                 4Ojs0q0XbtknTFN/3OXLkCKPRSNFqFEV0Oh2qamP4laYpKysrOI7D4cOHCYJAGU
                 nG647jMB6PldK+75Pn+YQS9REP0zKxfKSoj1HqiUrqIqFVaf/u3r2rFnFdlyeee
                 ELBYN++fWocb9s2g8FACSN5oj4HrapKUavMZmWoJv3x/TygywxGvrzU5zx1tpFy
                 WhhLdgn2brfL/Pw8gKpc6/dLLVTPMUEQ0G63CYJg4rooUe+Xud9gy3VdgiBQQ1t
                 ZqM4U9elbnbelkhUvDgYDBoOBGl5JEMp99XVk7fF4jK7rDIdDsixTgV2WJaPRiD
                 iOJ6rTpgLa6uoqnuepLy+ihOBePmpIjyyTh/q1JuU1M6tApV6CJ0mi4Dkej7Ftm
                 9FopOLx7t27SrFr165NQGhCgatXrypPyFcYmceL++rJrT6KFMqVglCUE1jFcTzx
                 /UCgKrEhn6B0XVfBHAQBURQBsLa2huM4fPvtt1NjAKC8fv16bNu2u3fvXubm5oj
                 jWM0/AZnJbJlY1ClW2AOYyAmSK+TYMAw6nQ5FUSjWEdbyfX/C6xIjn3/+Ob/++m
                 vMxjfjCQUSYDQcDm9cunTpb7du3VIzmvpIQ4RqHv/ZNRFkWsXZzPrTjgVuUqcNh
                 8MbwGhTZqVACNy8dOnS6UOHDrllWe4yTdOpf7CYJuCfCV2/537tYvO4eU68ulmj
                 JaPRaOXy5cungZubMisF1oF/pGn63xcvXlwD9gBd/lrbiA3Bfwb+wYbMG1/qARY
                 WFnRgG7CDDeHtf4aUf7KlbCixCgz6/f5Ga9b8d5uFhQWTDc/8Ff/ZI+/3+3n95P
                 8AnVIj0vD/0+QAAAAASUVORK5CYII=" alt=""/>
            DAL Generator&nbsp;
            <span style="font-size:20px;">v</span><?= $v_build_version ?>
        </h1>

        <?php if ($f_mode != 'generate' || $v_error) : ?>
            <?php if (!empty($a_msg)) : ?>
        <div id="message_box">
            <ul>
                <?php foreach ($a_msg as $v_msg) : ?>
                <li><?php echo htmlspecialchars($v_msg, ENT_QUOTES, 'utf-8') ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
            <?php endif; ?>

        <script type="text/javascript">
        //<![CDATA[
            function submit_form(form) {
                var el = document.getElementById('message_box');

                if(el) {
                    document.body.removeChild(el);
                }

                if(form) {
                    form.submit();
                }
            }
        //]]>
        </script>

        <form name="generator" id="generator" action="generator.php" method="POST" onsubmit="submit_form();">
            <label for="dbserver">Server        </label>
            <input type="text"      id="dbserver" name="dbserver" value="<?php echo $f_dbserver   ?>" size="30"/><br/><br/>
            <label for="dbuser"  >Username  </label>
            <input type="text"      id="dbuser"   name="dbuser"   value="<?php echo $f_dbuser     ?>" size="30" /><br/><br/>
            <label for="dbpasswd">Password      </label>
            <input type="password"  id="dbpasswd" name="dbpasswd" value="<?php echo $f_dbpasswd   ?>" size="30" value="" autocomplete="off"/><br/><br/>
            <label for="dbname"  >Database Name </label>
            <input type="text"      id="dbname"   name="dbname"   value="<?php echo $f_dbname     ?>" size="30" onchange="document.forms['generator'].genname.value=this.value"/><br/><br/>
            <label for="genname" >Generated Name </label>
            <input type="text"      id="genname"  name="genname"  value="<?php echo $f_genname    ?>" size="30"/><br/><br/>

            <input type="hidden"    name="mode"     value="generate" />
            <input type="submit" style="display:none;" />
            <div class="button" onclick="submit_form(document.forms['generator']);">
                <img src="
                     CAYAAAAf8/9hAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAN1wAADdcBQii
                     beAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAAKVSU
                     RBVDiNhZM/SBtxFMe/98tdEnPhLqWV5HIpQis42UlcbCE6FCIkECFLBw0lo
                     IsBK0SK15KaK1lSQTfnVtx0TJeUwg2l7VApFOw/EBsTvWAoShJzSX6/Do1Q
                     qJK3veH7eY/3/T4uk8mgV2ma5gAgAOgAIAA6uq6fo9v0EtsVRZkghLwSRTF
                     OCHnp9/uDmqbZAYDvNVlRlIlqtTrKGPter9dHARRPTk7uKorClcvlN7024E
                     3TjFiWZZckqRGNRvccDodlWZbbNM0HAPheADo3N/cewP74+Pjh9vZ2IRKJ/
                     ATwKx6PGwAod3FETdM4AByAvu5tKADOZrM9ppR+JYQEBEE4bbVa1ymlZULI
                     UKfTeUq6Yn55edmXy+VuyLIc4ThuVZbl8NbWlnNtbe1LOBx2U0oPms2mQCk
                     9mJyc7NvY2PgAoAPGGLe+vt7P8/wLQsgqIeQZgBgh5AkhJDswMPB8aWkp5P
                     f7kysrKwlFUR6l0+n7jDFfJpMBxxjjdnZ2+mdnZx9WKpVjAE0ANgCnAIiqq
                     t58Pv92eHj4FoAfAAYBfNJ1vQwAHGNM9Hq90bOzM6XRaBwGg8FrqVTqdzab
                     dRmGsS+Kok+SpHqpVHqt63rtP5sAENM0RwDsAnDGYrFyKBT6WCwWRwzDcNV
                     qtVKtVhvBFaEjAGg6nd4TRdED4HxhYWFIVdWh+fn5QQCWJEm+XC531HXlUk
                     Bjenr6nSzLAgDesqxqqVS612w2KwDsHo/HPTU1tQugcRWA5fP5Y0EQnKqq9
                     oui6ATwze12uwKBwE2e512FQuEIALsMYON5HouLi62ZmRlHMpmstttth2EY
                     3lQqdba5uVlMJBLm2NjYnq7r7csA/ybx4rFuA7gD4DP+2sZdJQaAP0XhCVX
                     9WNFfAAAAAElFTkSuQmCC" alt="" />
                Generate
            </div>
            <br/>
            <br/>
        </form>
        <br/>
        <a href="javascript:void(0)" onclick="document.getElementById('manual').style.display = 'block';">Show manual</a>
        <pre id="manual" class="code" style="display:none;">
#-------------------------------------------------------------------------------
# Usage
#-------------------------------------------------------------------------------
  * Provide database connection arguments and submit.
      - Server         : Server host name or IP address.
      - Username       : Database username.
      - Password       : Database password.
      - Database name  : Name of database.
      - Generated name : PHP file and class name.

  * If no errors occur, you will be asked to download a zip file which includes
      - db_GENNAME.php            : Contains tables and database structure.
      - lib_conn_GENNAME.php      : Contains database connect function.
      - lib_db_GENNAME.php        : Contains database configuration variables.
      - tbl_GENNAME_TABLENAME.php : Provides predefined variables and functions.

    GENNAME is 'Generated name'

  * Support Unicode in Column comments

#-------------------------------------------------------------------------------
# Dependencies
#-------------------------------------------------------------------------------
  * php_mbstring (PHP module)
  * lib_zip.php
  * lib_func.php

#-------------------------------------------------------------------------------
# Change log
#-------------------------------------------------------------------------------
  * v1.21 (2011-11-14)
      - Update UI: input height, line-height

  * v1.2 (2011-11-14)
      - Support arguments in column comments
        Syntax : ARGUMENT1=1&amp;ARGUMENT2=2
        e.g    : COMMENT=User ID&amp;INSTANCE=class_name

        Supported arguments:
            + COMMENT  : Column comment
            + INSTANCE : PHP class name (effects on table-class mapping in
                         db_GENNAME.php)

        Note: If column comments does NOT follow above syntax, it will be
              considered as COMMENT (backward compatibility).
              e.g. 'A comment' is equivalent to 'COMMENT=A comment'

      - Added 'Usage', 'Dependencies' and 'Change log'.

  * v1.13 (2011-11-14)
      - Changed UI

  * Older
      - Drafts
        </pre>
        <?php endif; ?>
    </body>
</html>
